package jp.sinmetal.trainer.service;

import java.util.List;

import jp.sinmetal.trainer.condition.EndBattleLogCondition;
import jp.sinmetal.trainer.entity.ConventionLog;
import static org.seasar.extension.jdbc.operation.Operations.desc;

/**
 * 大会ログサービス
 * @author sin
 *
 */
public class ConventionLogService extends AbstractService<ConventionLog> {

	/**
	 * 大会ログを返す
	 *
	 * @author sin
	 *
	 * @param conventionLogId
	 * @return
	 */
    public ConventionLog findById(Integer conventionLogId) {
        return select()
        		.innerJoin("convention")
        		.innerJoin("conventionMember1st")
        		.innerJoin("conventionMember1st.member")
        		.innerJoin("conventionMember2nd")
        		.innerJoin("conventionMember2nd.member")
        		.leftOuterJoin("winConventionMember")
        		.leftOuterJoin("winConventionMember.member")
        		.id(conventionLogId)
        		.getSingleResult();
    }

    /**
     * 対戦中の大会ログ一覧を返す
     * @author sin
     *
     * @param conventionId
     * @return
     */
    public List<ConventionLog> findDuringBattleAll(Integer conventionId) {
    	return select()
    			.innerJoin("conventionMember1st")
    			.innerJoin("conventionMember1st.member")
    			.innerJoin("conventionMember2nd")
    			.innerJoin("conventionMember2nd.member")
    			.where(SW()
    			.eq("conventionId", conventionId)
    			.eq("conventionLogState", ConventionLog.STATE_NORMAL))
    			.orderBy(desc("updateDate"))
    			.getResultList();
    }

    /**
     * 対戦終了した大会ログ数を返す
     * @author sin
     *
     * @param conventionId
     * @return
     */
    public long getEndBattleLogCount(Integer conventionId) {
    	return select()
				.where(SW()
					.eq("conventionId", conventionId)
					.ne("conventionLogState", ConventionLog.STATE_NORMAL))
				.getCount();
    }

    /**
     * 対戦終了した大会ログ一覧を返す
     * @author sin
     *
     * @param condition
     * @return
     */
    public List<ConventionLog> findEndBattleLogs(EndBattleLogCondition conditon) {
    	return select()
    			.innerJoin("conventionMember1st")
    			.innerJoin("conventionMember1st.member")
    			.innerJoin("conventionMember2nd")
    			.innerJoin("conventionMember2nd.member")
    			.leftOuterJoin("winConventionMember")
    			.leftOuterJoin("winConventionMember.member")
    			.where(SW()
    					.eq("conventionId", conditon.conventionId)
    					.ne("conventionLogState", ConventionLog.STATE_NORMAL))
    			.orderBy(desc("updateDate"))
    			.offset(conditon.offsetNum)
    			.limit(conditon.limitNum)
    			.getResultList();
    }

    /**
     * 大会ログ数を返す
     * @author sin
     *
     * @param conventionId
     * @return
     */
    public long getCount(Integer conventionId) {
    	return select().where(SW().eq("conventionId", conventionId)).getCount();
    }

    /**
     * マッチング数を返す
     * @author sin
     *
     * @param conventionId
     * @return
     */
    public long getMatchingCount(Integer conventionId) {
    	return select()
    			.where(SW()
    					.eq("conventionId", conventionId)
    					.eq("conventionLogState", ConventionLog.STATE_NORMAL))
    			.getCount();
    }

    /**
     * 複数の大会ログを挿入
     * @author sin
     *
     * @param entities
     * @return
     */
    public int[] insertItems(List<ConventionLog> entities) {
    	if (entities == null || entities.size() < 1) {
    		return new int[0];
    	}

    	int[] countArray = jdbcManager.insertBatch(entities).execute();
    	return countArray;
    }

    /**
     * 大会ログ更新
     * @author sin
     *
     * @param entity
     * @return
     */
    public int updateConventionLog(ConventionLog entity) {
    	return jdbcManager.update(entity).excludesNull().execute();
    }
}